/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gui;

import app.gui.scene.SceneHolder;
import app.gui.scene.SceneObserver;
import com.app.ThreadManager;
import com.app.game.control.HumanPlayer;
import com.app.server.Broadcast;
import com.app.server.Server;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 *
 * @author Maciej Strychalski
 * @author Rafał Tomaszewicz
 */
public class PlayerDialog extends SceneHolder{
    private TextField tf;
    private Button ok,cancel;
    private String labelString = "Imię gracza:";
    private SceneHolder prevHolder;
    private Broadcast bcast = new Broadcast(9090);
    private boolean local;
    
    public PlayerDialog(SceneHolder prevHolder,boolean local) {
        ThreadManager.getInstance().reset();
        this.local = local;
        this.previousHolder = prevHolder;
        VBox vb = new VBox(spacing/2);
        vb.setAlignment(Pos.CENTER);
        HBox hb = new HBox(spacing);
        hb.setAlignment(Pos.CENTER);
        Label lbl = new Label(labelString);
        tf = new TextField("Anonimowy Host");
        tf.setMaxWidth(buttonWidth);
        ok = new Button("Ok");
        ok.setMaxWidth(buttonWidth/2);
        ok.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
            	SceneHolder h;
            	Room room;
            	if(!PlayerDialog.this.local){
                room = new Room(PlayerDialog.this.previousHolder, new HumanPlayer(tf.getText()),false);
            	}else{
            		room = new Room(PlayerDialog.this.previousHolder, new HumanPlayer(tf.getText()),true);
            	}
                room.setHostMode(true);
                h=room;
                for(SceneObserver o:observers){
                    h.registerObserver(o);
                }
                nextScene = h.getScene();
                ThreadManager.getInstance().execute(bcast);
                notifyObservers();
                
            
            }
        });
        cancel = new Button("Powrót");
        cancel.setMaxWidth(buttonWidth/2);
        cancel.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                nextScene = PlayerDialog.this.prevHolder.getScene();
                notifyObservers();
            }
        });
        hb.getChildren().setAll(ok,cancel);
        vb.getChildren().setAll(lbl,tf,hb);
        scene = new Scene(vb, rootWidth, rootHeigh);
    }
    
    
}
